<?php
require_once 'vendor/autoload.php';
require 'helper.php';

date_default_timezone_set('Asia/Shanghai');
$options = getopt('',['uid:','debug']);

$options['debug'] = isset($options['debug']);

set_exception_handler(function (\Throwable $throwable)use($debug) {


    if ($throwable instanceof \GuzzleHttp\Exception\TransferException) {
        echo 'Request:' . PHP_EOL;
        print_r($throwable->getRequest()->getHeaders());
        print_r($throwable->getRequest()->getBody());
        if ($throwable instanceof \GuzzleHttp\Exception\RequestException) {
            echo 'Response:' . PHP_EOL;
            echo $throwable->getResponse()->getStatusCode().PHP_EOL;
            print_r($throwable->getResponse()->getHeaders());
            echo $throwable->getResponse()->getBody().PHP_EOL;
        }
    }



    echo 'Message:'.$throwable->getMessage().PHP_EOL;
    echo 'Code:'.$throwable->getCode().PHP_EOL;
    echo 'File:' . $throwable->getFile().PHP_EOL;
    echo 'Line:'.$throwable->getLine().PHP_EOL;
    echo 'Trace:'.$throwable->getTraceAsString().PHP_EOL;

    echo 'EXIT '.date("Y-m-d H:i:s").PHP_EOL;
});

$client = new BilibiliClient([
    'debug' => $options['debug']
]);

$offset = 0;

do{

    $ret = $client->dynamic_svr_space_history(['offset_dynamic_id' => $offset,'host_uid' => $options['uid']]);

    if ($ret['code'] !== 0) {
        print_r($ret);
        throw new \Exception('fail');
    }

    $getItem = function ($type,$card) {
        $ret = [];
        switch ($type) {
            case 0:

                break;

            case 1:
                $ret['content'] = $card['content'];
                $ret['timestamp'] = $card['timestamp'];
                break;

            case 2:
                //文字+图片动态

                $ret['description'] = $card['item']['description'];
                $ret['pictures'] = [];
                foreach ($card['item']['pictures'] as $pic) {
                    $ret['pictures'][] = $pic['img_src'];
                }
                break;
            case 4: //纯文字动态
                $ret['content'] = $card['content'];
                $ret['timestamp'] = $card['timestamp'];
                break;
            case 8:
                //视频
                $ret['dynamic'] = isset($card['dynamic']) ? $card['dynamic'] : ''; //动态内容
                $ret['aid'] = $card['aid']; // av号
                $ret['title'] = $card['title']; //标题
                $ret['pic'] = $card['pic']; // 封面
                $ret['desc'] = $card['desc'];// 视频简介
                $ret['timestamp'] = $card['ctime']; //时间
                break;

            case 64:
                //专栏
                $ret['dynamic'] = isset($card['dynamic']) ? $card['dynamic'] : ''; //动态内容
                $ret['id'] = $card['id']; // cv号
                $ret['title'] = $card['title']; //标题
                $ret['banner_url'] = $card['banner_url']; // 封面
                $ret['summary'] = $card['summary'];// 视频简介
                break;

            case 512: //番剧
            case 1024://视频已失效
                $ret['tips'] = $card['tips'];
                break;
            case 2048:
            default:

                break;
        }
        return $ret;
    };

    $parser = function ($card) use(&$getItem){

        $card['card'] = json_decode($card['card'], JSON_OBJECT_AS_ARRAY); // 动态内容
        $desc = $card['desc'];

        $ret = [
            'dy_id'=>$desc['dynamic_id_str'],
            'timestamp' => $desc['timestamp'],
            'type' => $desc['type'],
            'user' => $desc['user_profile']['info'],
            'item' => [],
            'orig_type' => $desc['orig_type'],
            'orig_dy_id_str'=>$desc['orig_dy_id_str'],
            'origin' => null,
            'origin_user' => null,
        ];

        switch ($desc['type']) {
            case 1:

                $ret['origin'] = $getItem($desc['orig_type'], json_decode($card['card']['origin'],JSON_OBJECT_AS_ARRAY) ) ;
                $ret['origin_user'] = $card['card']['item']['origin_user']['info'];
            default:
                $ret['item'] = $getItem($desc['type'], isset($card['card']['item']) ? $card['card']['item'] : $card['card']);
                break;
        }
        return $ret;
    };


    $arr = [];
    $data = $ret['data'];
    if (isset($data['cards']) ) {
        foreach ($data['cards'] as $card) {

            $arr[] = $parser($card);

            //$e['ctrl']; //array : data:uid,type:1 @的人uid  type:3 投票链接

            //$c['user'];// face uid uname
            //$c['item'];//
            //orig_type 512 番剧
            // uid content  orig_dy_id timestamp orig_type 64专栏
            // id pictures.img_src pictures_count description orig_type 2文字+图片
            // uid orig_dy_id timestamp content orig_type 8视频动态  aid av号 dynamic动态内容 jump_url链接 owner投稿人信息 title视频标题 pic封面
            // orig_type  1纯文字
            // orig_type 4 无转发的纯文字动态  投票动态 orig_dy_id timestamp content origin_extension.vote_cfg.vote_id   origin_extension.vote_cfg。desc  待定
            // orig_type 1024 tips 视频资源已失效
            // type 2048 活动链接/直播间？  sketch.target_url sketch.title  sketch.desc_text sketch.cover_url
            //$c['origin'];//
            // id title summary  banner_url  转发内容 为专栏动态
            //
            // $c['origin_user']['info'];// uid uname face
        }
        $offset = $card['desc']['dynamic_id_str'];
    }



}while ($data['has_more'] !== 0);
